package cn.factorybox.item.utils;

import cn.factorybox.model.entity.DeviceData;
import cn.factorybox.model.entity.JudgeValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-04-08 12:37
 */
public class DataStatusCount {

    // 判断室内外温差、湿差、压差的标准
    public static String tempReduce(Double data) {
//        data = Math.abs(data);
        if (data >= 0 && data < 6) {
            return "正常";
        } else {
            return "异常";
        }
    }

    public static String humReduce(Double data) {
//        data = Math.abs(data);
        if (data >= 0 && data < 30) {
            return "正常";
        } else {
            return "异常";
        }
    }

    public static String pressReduce(Double data,JudgeValue judgeValue,int type) {
        String  status = "";
        if (type == 0) {
            if (data >= judgeValue.getYouxiuMin() && data <= judgeValue.getYouxiuMax()) {
                status = "正常";
            } else if ((data >= judgeValue.getLianghaoMin1() && data < judgeValue.getLianghaoMax1()) || (data > judgeValue.getLianghaoMin2() && data <= judgeValue.getLianghaoMax2())) {
                status = "正常";
            } else if ((data >= judgeValue.getYibanMin1() && data < judgeValue.getYibanMax1()) || (data > judgeValue.getYibanMin2() && data <= judgeValue.getYibanMax2())) {
                status = "正常";
            } else if (data < judgeValue.getWeixianMin() || data > judgeValue.getWeixianMax()) {
                status = "异常";
            }
        }else if (type == 1) {
            if (data >= judgeValue.getYouxiuMin()) {
                status = "正常";
            } else if (data >= judgeValue.getLianghaoMin1() && data < judgeValue.getLianghaoMax1()) {
                status = "正常";
            } else if (data >= judgeValue.getYibanMin1() && data < judgeValue.getYibanMax1()) {
                status = "正常";
            } else if (data < judgeValue.getWeixianMin()) {
                status = "异常";
            }
        }else if (type == 2) {
            if (data >= judgeValue.getYouxiuMin() && data <= judgeValue.getYouxiuMax()) {
                status = "正常";
            } else if ((data >= judgeValue.getLianghaoMin1() && data < judgeValue.getLianghaoMax1()) || (data > judgeValue.getLianghaoMin2() && data <= judgeValue.getLianghaoMax2())) {
                status = "正常";
            } else if ((data >= judgeValue.getYibanMin1() && data < judgeValue.getYibanMax1()) || (data > judgeValue.getYibanMin2() && data <= judgeValue.getYibanMax2())) {
                status = "正常";
            } else if (data < judgeValue.getWeixianMin() || data > judgeValue.getWeixianMax()) {
                status = "异常";
            }
        }
        return status;
    }

    public static int getGreatCount(List<DeviceData> list){
        int count = 0;
        for (DeviceData deviceData : list) {
            if (deviceData.getResult().equals("优秀")) {
                count ++;
            }
        }
        return count;
    }

    public static int getNormalCount(List<DeviceData> list){
        int count = 0;
        for (DeviceData deviceData : list) {
            if (deviceData.getResult().equals("良好")) {
                count ++;
            }
        }
        return count;
    }


    public static int getInfoCount(List<DeviceData> list){
        int count = 0;
        for (DeviceData deviceData : list) {
            if (deviceData.getResult().equals("一般")) {
                count ++;
            }
        }
        return count;
    }


    public static int getWarnCount(List<DeviceData> list){
        int count = 0;
        for (DeviceData deviceData : list) {
            if (deviceData.getResult().equals("不适")) {
                count ++;
            }
        }
        return count;
    }

    public static Map<String,Object> getStatus(List<DeviceData> data, int type, List<JudgeValue> list) {
        Map<String,Object> map = new HashMap<>();
        map.put("allcount",data.size());
        //数组内分别为温度、湿度、二氧化碳、tvoc、排气、压差、PM2.5
        int[] greatcount = new int[7];
        int[] normalcount = new int[7];
        int[] infocount = new int[7];
        int[] warncount = new int[7];

        for (DeviceData deviceData : data) {
            if (type == 0) {
                judgeTemperature_indoor(deviceData.getfTemperature(),greatcount,normalcount,infocount,warncount,list.get(0));
                judgeHumidity_indoor(deviceData.getfHumidity(),greatcount,normalcount,infocount,warncount,list.get(1));
                judgePressure_indoor(deviceData.getfPressure(),greatcount,normalcount,infocount,warncount,list.get(2));
                judgeExhaust_indoor(deviceData.getfExhaust(),greatcount,normalcount,infocount,warncount,list.get(3));
                judgePM_indoor(deviceData.getfPm(),greatcount,normalcount,infocount,warncount,list.get(4));
                judgeCO2_indoor(deviceData.getfCarbonDioxide(),greatcount,normalcount,infocount,warncount,list.get(5));
                judgeTVOC_indoor(deviceData.getfTvoc(),greatcount,normalcount,infocount,warncount,list.get(6));
            }else if (type == 1) {
                judgeTemperature_exper(deviceData.getfTemperature(),greatcount,normalcount,infocount,warncount,list.get(0));
                judgeHumidity_exper(deviceData.getfHumidity(),greatcount,normalcount,infocount,warncount,list.get(1));
                judgePressure_exper(deviceData.getfPressure(),greatcount,normalcount,infocount,warncount,list.get(2));
                judgeExhaust_exper(deviceData.getfExhaust(),greatcount,normalcount,infocount,warncount,list.get(3));
                judgePM_exper(deviceData.getfPm(),greatcount,normalcount,infocount,warncount,list.get(4));
                judgeCO2_exper(deviceData.getfCarbonDioxide(),greatcount,normalcount,infocount,warncount,list.get(5));
                judgeTVOC_exper(deviceData.getfTvoc(),greatcount,normalcount,infocount,warncount,list.get(6));
            }else if (type == 2) {
                judgeTemperature_outdoor(deviceData.getfTemperature(),greatcount,normalcount,infocount,warncount,list.get(0));
                judgeHumidity_outdoor(deviceData.getfHumidity(),greatcount,normalcount,infocount,warncount,list.get(1));
                judgePressure_outdoor(deviceData.getfPressure(),greatcount,normalcount,infocount,warncount,list.get(2));
                judgeExhaust_outdoor(deviceData.getfExhaust(),greatcount,normalcount,infocount,warncount,list.get(3));
                judgePM_outdoor(deviceData.getfPm(),greatcount,normalcount,infocount,warncount,list.get(4));
                judgeCO2_outdoor(deviceData.getfCarbonDioxide(),greatcount,normalcount,infocount,warncount,list.get(5));
                judgeTVOC_outdoor(deviceData.getfTvoc(),greatcount,normalcount,infocount,warncount,list.get(6));
            }
        }

        int greatnum = 0;
        int normalnum = 0;
        int infonum = 0;
        int warnnum = 0;
        for (DeviceData deviceData : data) {
            if (deviceData.getResult().equals("优秀")) {
                greatnum ++;
            }
            if (deviceData.getResult().equals("良好")) {
                normalnum ++;
            }
            if (deviceData.getResult().equals("一般")) {
                infonum ++;
            }
            if (deviceData.getResult().equals("不适")) {
                warnnum ++;
            }
        }

        map.put("greatcount",greatcount);
        map.put("normalcount",normalcount);
        map.put("infocount",infocount);
        map.put("warncount",warncount);
        map.put("greatnum",greatnum);
        map.put("normalnum",normalnum);
        map.put("infonum",infonum);
        map.put("warnnum",warnnum);

        return map;
    }



    /******************************室内判断*****************************/

    /**
     * 判断温度
     * @param m 温度值
     */
    public static void judgeTemperature_indoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv1) {
        if (m >= jv1.getYouxiuMin() && m <= jv1.getYouxiuMax()) {
            great[0] ++;
        } else if ((m >= jv1.getLianghaoMin1() && m < jv1.getLianghaoMax1()) || (m > jv1.getLianghaoMin2() && m <= jv1.getLianghaoMax2())) {
            nomal[0] ++;
        } else if ((m >= jv1.getYibanMin1() && m < jv1.getYibanMax1()) || (m > jv1.getYibanMin2() && m <= jv1.getYibanMax2())) {
            info[0] ++;
        } else if (m < jv1.getWeixianMin() || m > jv1.getWeixianMax()) {
            warn[0] ++;
        }
    }

    /**
     * 判断湿度
     * @param m 湿度值
     */
    public static void judgeHumidity_indoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv2) {
        if (m >= jv2.getYouxiuMin() && m <= jv2.getYouxiuMax()) {
            great[1] ++;
        } else if ((m >= jv2.getLianghaoMin1() && m < jv2.getLianghaoMax1()) || (m > jv2.getLianghaoMin2() && m <= jv2.getLianghaoMax2())) {
            nomal[1] ++;
        } else if ((m >= jv2.getYibanMin1() && m < jv2.getYibanMax1()) || (m > jv2.getYibanMin2() && m <= jv2.getYibanMax2())) {
            info[1] ++;
        } else if (m < jv2.getWeixianMin() || m > jv2.getWeixianMax()) {
            warn[1] ++;
        }
    }

    /**
     * 判断室内压强
     * @param m 室内压强
     */
    public static void judgePressure_indoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue judgeValue) {
        if (m >= judgeValue.getYouxiuMin() && m <= judgeValue.getYouxiuMax()) {
            great[2] ++;
        } else if ((m >= judgeValue.getLianghaoMin1() && m < judgeValue.getLianghaoMax1()) || (m > judgeValue.getLianghaoMin2() && m <= judgeValue.getLianghaoMax2())) {
            nomal[2] ++;
        } else if ((m >= judgeValue.getYibanMin1() && m < judgeValue.getYibanMax1()) || (m > judgeValue.getYibanMin2() && m <= judgeValue.getYibanMax2())) {
            info[2] ++;
        } else if (m < judgeValue.getWeixianMin() || m > judgeValue.getWeixianMax()) {
            warn[2] ++;
        }
    }


    /**
     * 判断排气量
     * @param m 排气量
     */
    public static void judgeExhaust_indoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv4) {
        if (m >= jv4.getYouxiuMin() && m <= jv4.getYouxiuMax()) {
            great[3] ++;
        } else if ((m >= jv4.getLianghaoMin1() && m < jv4.getLianghaoMax1()) || (m > jv4.getLianghaoMin2())) {
            nomal[3] ++;
        } else if (m >= jv4.getYibanMin1() && m < jv4.getYibanMax1()) {
            info[3] ++;
        } else if (m >= jv4.getWeixianMin() && m < jv4.getWeixianMax()) {
            warn[3] ++;
        }
    }

    /**
     * 判断PM2.5
     * @param m PM2.5
     */
    public static void judgePM_indoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv5) {
        if (m >= jv5.getYouxiuMin() && m <= jv5.getYouxiuMax()) {
            great[4] ++;
        } else if (m > jv5.getLianghaoMin1() && m <= jv5.getLianghaoMax1()){
            nomal[4] ++;
        } else if (m > jv5.getYibanMin1() && m <= jv5.getYibanMax1()) {
            info[4] ++;
        } else if (m > jv5.getWeixianMin()) {
            warn[4] ++;
        }
    }

    public static void judgeCO2_indoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv6) {
        if (m >= jv6.getYouxiuMin() && m <= jv6.getYouxiuMax()) {
            great[5] ++;
        } else if (m > jv6.getLianghaoMin1() && m <= jv6.getLianghaoMax1()) {
            nomal[5] ++;
        } else if (m > jv6.getYibanMin1() && m <= jv6.getYibanMax1()) {
            info[5] ++;
        } else if (m > jv6.getWeixianMin()) {
            warn[5] ++;
        }
    }

    public static void judgeTVOC_indoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv7) {
        if (m >= jv7.getYouxiuMin() && m <= jv7.getYouxiuMax()) {
            great[6] ++;
        } else if (m > jv7.getLianghaoMin1() && m <= jv7.getLianghaoMax1()) {
            nomal[6] ++;
        } else if (m > jv7.getYibanMin1() && m <= jv7.getYibanMax1()) {
            info[6] ++;
        } else if (m > jv7.getWeixianMin()) {
            warn[6] ++;
        }
    }



/******************************实验室判断****************************/

    /**
     * 判断温度
     * @param m 温度值
     */
    public static void judgeTemperature_exper(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv1) {
        if (m >= jv1.getYouxiuMin() && m <= jv1.getYouxiuMax()) {
            great[0] ++;
        } else if ((m >= jv1.getLianghaoMin1() && m < jv1.getLianghaoMax1()) || (m > jv1.getLianghaoMin2() && m <= jv1.getLianghaoMax2())) {
            nomal[0] ++;
        } else if ((m >= jv1.getYibanMin1() && m < jv1.getYibanMax1()) || (m > jv1.getYibanMin2() && m <= jv1.getYibanMax2())) {
            info[0] ++;
        } else if (m < jv1.getWeixianMin() || m > jv1.getWeixianMax()) {
            warn[0] ++;
        }
    }

    /**
     * 判断湿度
     * @param m 湿度值
     */
    public static void judgeHumidity_exper(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv2) {
        if (m >= jv2.getYouxiuMin() && m <= jv2.getYouxiuMax()) {
            great[1] ++;
        } else if ((m >= jv2.getLianghaoMin1() && m < jv2.getLianghaoMax1()) || (m > jv2.getLianghaoMin2() && m <= jv2.getLianghaoMax2())) {
            nomal[1] ++;
        } else if ((m >= jv2.getYibanMin1() && m < jv2.getYibanMax1()) || (m > jv2.getYibanMin2() && m <= jv2.getYibanMax2())) {
            info[1] ++;
        } else if (m < jv2.getWeixianMin() || m > jv2.getWeixianMax()) {
            warn[1] ++;
        }
    }

    /**
     * 判断室内压强
     * @param m 室内压强
     */
    public static void judgePressure_exper(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue judgeValue) {
        if (m >= judgeValue.getYouxiuMin()) {
            great[2] ++;
        } else if (m >= judgeValue.getLianghaoMin1() && m < judgeValue.getLianghaoMax1()) {
            nomal[2] ++;
        } else if (m >= judgeValue.getYibanMin1() && m < judgeValue.getYibanMax1()) {
            info[2] ++;
        } else if (m < judgeValue.getWeixianMin()) {
//            System.out.println("压差：" + m + "," + judgeValue.getWeixianMin() + "," + judgeValue.getWeixianMax());
            warn[2] ++;
        }
    }


    /**
     * 判断排气量
     * @param m 排气量
     */
    public static void judgeExhaust_exper(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv4) {
        if (m >= jv4.getYouxiuMin() && m <= jv4.getYouxiuMax()) {
            great[3] ++;
        } else if ((m >= jv4.getLianghaoMin1() && m < jv4.getLianghaoMax1()) || (m > jv4.getLianghaoMin2())) {
            nomal[3] ++;
        } else if (m >= jv4.getYibanMin1() && m < jv4.getYibanMax1()) {
            info[3] ++;
        } else if (m > jv4.getWeixianMin() && m < jv4.getWeixianMax()) {
            warn[3] ++;
        }
    }

    /**
     * 判断PM2.5
     * @param m PM2.5
     */
    public static void judgePM_exper(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv5) {
        if (m >= jv5.getYouxiuMin() && m <= jv5.getYouxiuMax()) {
            great[4] ++;
        } else if (m > jv5.getLianghaoMin1() && m <= jv5.getLianghaoMax1()){
            nomal[4] ++;
        } else if (m > jv5.getYibanMin1() && m <= jv5.getYibanMax1()) {
            info[4] ++;
        } else if (m > jv5.getWeixianMin()) {
            warn[4] ++;
        }
    }

    public static void judgeCO2_exper(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv6) {
        if (m >= jv6.getYouxiuMin() && m <= jv6.getYouxiuMax()) {
            great[5] ++;
        } else if (m > jv6.getLianghaoMin1() && m <= jv6.getLianghaoMax1()) {
            nomal[5] ++;
        } else if (m > jv6.getYibanMin1() && m <= jv6.getYibanMax1()) {
            info[5] ++;
        } else if (m > jv6.getWeixianMin()) {
            warn[5] ++;
        }
    }

    public static void judgeTVOC_exper(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv7) {
        if (m >= jv7.getYouxiuMin() && m <= jv7.getYouxiuMax()) {
            great[6] ++;
        } else if (m > jv7.getLianghaoMin1() && m <= jv7.getLianghaoMax1()) {
            nomal[6] ++;
        } else if (m > jv7.getYibanMin1() && m <= jv7.getYibanMax1()) {
            info[6] ++;
        } else if (m > jv7.getWeixianMin()) {
            warn[6] ++;
        }
    }

/************************室外判断*************************/

    /**
     * 判断温度
     * @param m 温度值
     */
    public static void judgeTemperature_outdoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv1) {
        if (m >= jv1.getYouxiuMin() && m <= jv1.getYouxiuMax()) {
            great[0] ++;
        } else if ((m >= jv1.getLianghaoMin1() && m < jv1.getLianghaoMax1()) || (m > jv1.getLianghaoMin2() && m <= jv1.getLianghaoMax2())) {
            nomal[0] ++;
        } else if ((m >= jv1.getYibanMin1() && m < jv1.getYibanMax1()) || (m > jv1.getYibanMin2() && m <= jv1.getYibanMax2())) {
            info[0] ++;
        } else if (m < jv1.getWeixianMin() || m > jv1.getWeixianMax()) {
            warn[0] ++;
        }
    }

    /**
     * 判断湿度
     * @param m 湿度值
     */
    public static void judgeHumidity_outdoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv2) {
        if (m >= jv2.getYouxiuMin() && m <= jv2.getYouxiuMax()) {
            great[1] ++;
        } else if ((m >= jv2.getLianghaoMin1() && m < jv2.getLianghaoMax1()) || (m > jv2.getLianghaoMin2() && m <= jv2.getLianghaoMax2())) {
            nomal[1] ++;
        } else if ((m >= jv2.getYibanMin1() && m < jv2.getYibanMax1()) || (m > jv2.getYibanMin2() && m <= jv2.getYibanMax2())) {
            info[1] ++;
        } else if (m < jv2.getWeixianMin() || m > jv2.getWeixianMax()) {
            warn[1] ++;
        }
    }

    /**
     * 判断室内压差
     * @param m 室内压差
     */
    public static void judgePressure_outdoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue judgeValue) {
        if (m >= judgeValue.getYouxiuMin() && m <= judgeValue.getYouxiuMax()) {
            great[2] ++;
        } else if ((m >= judgeValue.getLianghaoMin1() && m < judgeValue.getLianghaoMax1()) || (m > judgeValue.getLianghaoMin2() && m <= judgeValue.getLianghaoMax2())) {
            nomal[2] ++;
        } else if ((m >= judgeValue.getYibanMin1() && m < judgeValue.getYibanMax1()) || (m > judgeValue.getYibanMin2() && m <= judgeValue.getYibanMax2())) {
            info[2] ++;
        } else if (m < judgeValue.getWeixianMin() || m > judgeValue.getWeixianMax()) {
            warn[2] ++;
        }
    }


    /**
     * 判断排气量
     * @param m 排气量
     */
    public static void judgeExhaust_outdoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv4) {
        if (m >= jv4.getYouxiuMin() && m <= jv4.getYouxiuMax()) {
            great[3] ++;
        } else if ((m >= jv4.getLianghaoMin1() && m < jv4.getLianghaoMax1()) || (m > jv4.getLianghaoMin2() && m <= jv4.getLianghaoMax2())) {
            nomal[3] ++;
        } else if ((m >= jv4.getYibanMin1() && m < jv4.getYibanMax1()) || (m > jv4.getYibanMin2() && m <= jv4.getYibanMax2())) {
            info[3] ++;
        } else if (m >= 0 && m < jv4.getWeixianMin() || m > jv4.getWeixianMax()) {
            warn[3] ++;
        }
    }

    /**
     * 判断PM2.5
     * @param m PM2.5
     */
    public static void judgePM_outdoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv5) {
        if (m >= jv5.getYouxiuMin() && m <= jv5.getYouxiuMax()) {
            great[4] ++;
        } else if (m > jv5.getLianghaoMin1() && m <= jv5.getLianghaoMax1()){
            nomal[4] ++;
        } else if (m > jv5.getYibanMin1() && m <= jv5.getYibanMax1()) {
            info[4] ++;
        } else if (m > jv5.getWeixianMin()) {
            warn[4] ++;
        }
    }

    public static void judgeCO2_outdoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv6) {
        if (m >= jv6.getYouxiuMin() && m <= jv6.getYouxiuMax()) {
            great[5] ++;
        } else if (m > jv6.getLianghaoMin1() && m <= jv6.getLianghaoMax1()) {
            nomal[5] ++;
        } else if (m > jv6.getYibanMin1() && m <= jv6.getYibanMax1()) {
            info[5] ++;
        } else if (m > jv6.getWeixianMin()) {
            warn[5] ++;
        }
    }

    public static void judgeTVOC_outdoor(double m,int[] great,int[] nomal,int[] info,int[] warn,JudgeValue jv7) {
        if (m >= jv7.getYouxiuMin() && m <= jv7.getYouxiuMax()) {
            great[6] ++;
        } else if (m > jv7.getLianghaoMin1() && m <= jv7.getLianghaoMax1()) {
            nomal[6] ++;
        } else if (m > jv7.getYibanMin1() && m <= jv7.getYibanMax1()) {
            info[6] ++;
        } else if (m > jv7.getWeixianMin()) {
            warn[6] ++;
        }
    }
}
